/**
 *
 * 依赖 config,$$
 *
 * Created by znsw007 on 2015/12/18.
 */

var utils = {
    renderTplById: function (id, data) {
        var markup = $$("#" + id).html();
        var compiledTemplate = Template7.compile(markup);
        return compiledTemplate(data);
    },
    bindEvent: function (bindings) {
        for (var i in bindings) {
            if (bindings[i].selector) {
                $$(bindings[i].element).on(bindings[i].event, bindings[i].selector, bindings[i].handler);
            } else {
                $$(bindings[i].element).on(bindings[i].event, bindings[i].handler);
            }
        }
    },
    /**
     * 获取客户端传给页面的versioncode
     * 版本转换（兼容老版本）
     * @returns {*}
     */
    clientVersion: function () {
        /*10没有意义，只是占位
         310是版本号
         1是正式版，0是公测版
         01是安卓，02是苹果
         Android 315的公测版就是10315001，315正式版就是10315101*/
        var versioncode = xhr.seach('versioncode') || '',
            reg = /^\d+$/,
            v,
            device,
            ios = "IOS",
            android = "ANDROID";

        if (!reg.test(versioncode)) { //说明是老版本
            var t = ['10'];
            v = versioncode.replace(/\./g, ''),
                t.push(v);//版本号
            t.push('1');//默认是正式版
            if (xhr.seach('versioncode') && xhr.seach('versioncode') == 'IOS') {
                t.push('02');
                device = ios;
            } else {
                t.push("01"); //android;
                device = android;
            }
            versioncode = t.join("");
        } else {
            device = versioncode.substring(versioncode.length - 2) == '01' ? android : ios;
            v = (versioncode.substring(2, versioncode.length - 3) - 0);
        }
        return {
            versioncode: versioncode,
            vcode: (v - 0),
            device: device
        };
        // return versioncode;
    },
    /**
     *
     * @param cfg
     * @param callback
     */
    toast: function (cfg, callback) {
        var defa = {
            timer: 2 * 1000,
            msg: '',
        }, self = this;
        if (typeof (cfg) == 'string') {
            defa['msg'] = cfg;
        } else if (typeof (cfg) == 'object') {
            for (var i in cfg) defa[i] = cfg[i];
        }

        if (!self._toastStack) {
            self._isShow = false;
            self._toastStack = [];
        }
        self._toastStack.push(t(defa, callback));

        notify();

        function toastStackClearQueue() {
            if (self._toastStack.length) {
                (self._toastStack.shift())();
            }
        }

        function t(config, callback) {
            var tpl = '<div class="toast toast-in">' +
                '<div class="inner">' + config.msg +
                '</div>'
            '</div>',
                c = function () {
                    callback && callback();
                };

            return function () {
                $$('body').append(tpl);
                self._isShow = true;
                setTimeout(function () {
                    $$('.toast-in').remove();
                    c();
                    self._isShow = false;
                    notify();
                }, config.timer);
            }
        }

        function notify() {
            if (!self._isShow) {
                toastStackClearQueue();
            }
        }
    },
    iosVersion: function () {

    },
    isIos7: function () {
        return true;
    },
    isEmpty: function (sprama) {
        if (typeof (sprama) == "undefined" || !sprama || sprama == '') {
            return true;
        }
        return sprama.length === 0;
    },
    isArrInval: function (arrs) {
        if (arrs == "null" || arrs == "" || this.isEmpty(arrs)) {
            return true;
        }
        return false;
    },
    isMobilePhone: function (param) {
        var tel = param;
        //^0?1[3458]\\d{9}$
        var reg = /^0?1[3|4|5|7|8]\d{9}$/;
        if (reg.test(tel)) {
            return true;
        } else {
            return false;
        };
    },
    formatTimeStamp: function (stamp) {
        return moment(stamp).format("YYYY-MM-DD hh:mm:ss");
    },
    isEqual: function (a, b) {
        return eq(a, b);
    },
};
